@echo off
setlocal

REM 设置项目根目录
cd /d "%~dp0"

REM 显示帮助信息
if "%1"=="" (
    echo Usage: makefile.bat [target]
    echo.
    echo Available targets:
    echo   build     构建项目
    echo   run       运行项目
    echo   migrate   数据库迁移
    echo   test      运行测试
    echo   clean     清理构建文件
    echo   help      显示此帮助信息
    goto :eof
)

REM 处理不同的目标
if /i "%1"=="build" goto :build
if /i "%1"=="run" goto :run
if /i "%1"=="migrate" goto :migrate
if /i "%1"=="test" goto :test
if /i "%1"=="clean" goto :clean
if /i "%1"=="help" goto :help

echo Unknown target: %1
echo Use 'makefile.bat help' for available targets
exit /b 1

:build
echo Building project...
go build -o bin/server.exe cmd/server/main.go
go build -o bin/migrate.exe scripts/migrate.go
copy config.yaml bin\config.yaml
echo Build completed.
goto :eof

:run
echo Running project...
if exist bin\server.exe (
    bin\server.exe
) else (
    echo Server executable not found. Please run 'makefile.bat build' first.
)
goto :eof

:migrate
echo Running database migration...
if exist bin\migrate.exe (
    bin\migrate.exe
) else (
    echo Migration executable not found. Please run 'makefile.bat build' first.
)
goto :eof

:test
echo Running tests...
go test ./...
goto :eof

:clean
echo Cleaning build files...
del bin\server.exe bin\migrate.exe 2>nul
echo Clean completed.
goto :eof

:help
echo Usage: makefile.bat [target]
echo.
echo Available targets:
echo   build     构建项目
echo   run       运行项目
echo   migrate   数据库迁移
echo   test      运行测试
echo   clean     清理构建文件
echo   help      显示此帮助信息
goto :eof